package com.babasport.web.action.product;

import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.babasport.bean.PageView;
import com.babasport.bean.QueryResult;
import com.babasport.bean.product.ProductType;
import com.babasport.service.product.ProductInfoService;
import com.babasport.web.formbean.product.ProductInfoForm;


@Controller("/control/product/manage")
public class ProductInfoAction extends DispatchAction {

	private static final Logger logger = Logger
			.getLogger(ProductInfoAction.class);

	@Resource(name = "productInfoServiceImpl")
	private ProductInfoService service;

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductInfoForm ProductInfoForm = (ProductInfoForm) form;
		PageView<ProductType> pageView = new PageView<ProductType>(12,
				ProductInfoForm.getPage());
		int firstIndex = (pageView.getCurrentpage() - 1)
				* pageView.getMaxresult();
		
		HashMap<String, Object> where = new HashMap<String, Object>();
		where.put("visible=", true);
//		if (ProductInfoForm.getParentid() != null
//				&& ProductInfoForm.getParentid() > 0) {
//			where.put("parent=", ProductInfoForm.getParentid());
//		} else {
//			where.put("parent=", null);
//		}
		
		if (ProductInfoForm.getName() != null
				&& !"".equals(ProductInfoForm.getName().trim())) {
			where.put("name like '%", ProductInfoForm.getName().trim() + "%'");
		}
		
		LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
		orderBy.put("typeId", "desc");
		
		QueryResult<ProductType> qr = service.getScrollData(
				ProductType.class, firstIndex, pageView.getMaxresult(), where,
				orderBy);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		return mapping.findForward("listUI");
	}
	
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("addUI");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductInfoForm ProductInfoForm = (ProductInfoForm) form;
		ProductType productType = null; //new ProductType(ProductInfoForm.getName(), ProductInfoForm.getNote());
//		if(ProductInfoForm.getParentid() != null && ProductInfoForm.getParentid() > 0){
//			productType.setParent(new ProductType(ProductInfoForm.getParentid()));
//		}
		service.save(productType);
		request.setAttribute("message", "添加类型成功!");
		request.setAttribute("urladdress", "/control/product/type/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductInfoForm ProductInfoForm = (ProductInfoForm) form;
		ProductType productType = service.query(ProductType.class, ProductInfoForm.getTypeid());
//		ProductInfoForm.setName(productType.getName());
//		ProductInfoForm.setNote(productType.getNote());
		return mapping.findForward("editUI");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductInfoForm ProductInfoForm = (ProductInfoForm) form;
		ProductType productType = service.query(ProductType.class, ProductInfoForm.getTypeid());
//		productType.setName(ProductInfoForm.getName());
//		productType.setNote(ProductInfoForm.getNote());
		service.update(productType);
		request.setAttribute("message", "修改类型成功!");
		request.setAttribute("urladdress", "/control/product/type/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("queryUI");
	}
}
